package com.yusvn.scm.goods.buhuo.controller;

import com.jfinal.aop.Inject;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Record;
import com.yusvn.scm.common.BaseController;
import com.yusvn.scm.common.kit.MyKit;
import com.yusvn.scm.common.kit.RetKit;
import com.yusvn.scm.goods.buhuo.service.ScmBuhuoMdbhService;
import com.yusvn.scm.goods.buhuo.service.ScmBuhuoZbbhService;


/**
 * 总部补货
 *
 * @author liugz
 */
@EnableCORS
public class ScmBuhuoZbbhController extends BaseController {

    @Inject
    ScmBuhuoZbbhService srv;
    @Inject
    ScmBuhuoMdbhService mdsrv;

    public void index() {
        renderJson();
    }

    /**
     * 总部补货查询
     */
    public void selectALl() {
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        //kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        
        renderJson(srv.selectALlBh(kv));
    }

    /**
     * 查询总部本次补货明细
     */
    public void selectBcbhmx() {
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        //kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        
        renderJson(srv.selectBcbhmx(kv));
    }

    /**
     * 门店提交情况
     */
    public void mdCommit() {
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        //kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        renderJson(srv.mdCommit(kv));
    }
    
    /**
     * 获取所有店某个商品的颜色尺码，用于新增
     */
    public void getSpyscm() {
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        
        renderJson(RetKit.ok().setData(srv.getSpyscm(kv)));
    }
    
    /**
     * 获取某个商品补货数据， 用于修改补货数量
     */
    public void selectBhmx() {
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        //kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        
        renderJson(srv.selectBhmx(kv));
    }

    /**
     * 批量删除不需要补货的商品
     */
    public void deletePl() {
        renderJson(srv.deletePlsp(getRawData()));
    }

    /**
     * 总部确认某商品补货
     */
    public void zbbhQrsp() {
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        //kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());

        try {
			RetKit ret = srv.zbbhQrsp(kv);
			renderJson(ret );
		} catch (Exception e1) {
			e1.printStackTrace();
			renderJson(RetKit.fail("确认失败"));
		}
    }
    
    /**
     * 开启审核
     */
    public void toExamine() {
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        //kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        
        renderJson(srv.toExamine(kv));
    }

    /**
     * 关闭审核
     */
    public void toExamineQx() {
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        //kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        
        renderJson(srv.toExamineQx(kv));
    }

    /**
     * 生成单据
     */
    public void scdj() {
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        
        renderJson(srv.scdj(kv));
    }

    /**
     * 总部修改数量
     */
    public void updateZbbhsl() {
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.updateZbbhsl(kv));
    }

    /**
     * 增加某款色门店的补货数量
     */
    public void insertStore() {
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.insertStore(kv));
    }

    /**
     * 总部新增补货
     */
    public void insertZbbh() {
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.insertZbbh(kv));
    }

    /**
     * 获取补货进度、补货时间
     */
    public void getBhsj() {
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        
        Record rec = srv.getBhsj(kv);
        renderJson(RetKit.ok().setData(rec));
    }
    
    /**
     * 总部表2 总部代替确认某门店可以补货
     */
    public void updateSftjqr(){
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }

        renderJson(srv.updateSftjqr(kv));
    }
    
    /**
     * 获取商品动态属性对应表
     */
    public void selectAllAttrib() {
        renderJson(mdsrv.selectAllAttrib());
    }
    
    /**
     * 获取某个商品属性
     */
    public void selctAttribById() {
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(mdsrv.selctAttribById(kv));
    }


    /**
     * 打印数据查询
     */
    public void zbdycx(){
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        renderJson(srv.zbdycx(kv));
    }

    /**
     * 修改打印次数
     */
    public void xgdycs(){
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        renderJson(srv.xgdycs(kv));
    }

    public void cxZbkscgsl(){
        Kv kv = jsonStrToKv(getRawData());
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.cxZbkscgsl(kv));
    }

    /**
     * 获取商品图片和供应商
     */
    public void getPstpGys(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.getPstpGys(kv));
    }

    /**
     * 总部打印选择列
     */
    public void dycxAll(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        renderJson(srv.dycxAll(kv));
    }

    /**
     * 总部查询打印数据
     */
    public void zbcxDysj(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.zbcxDysj(kv));
    }

    /**
     * 查询补货打印抬头
     */
    public void dytt(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.dytt(kv));
    }

    /**
     * 总部查询补货明细历史记录
     */
    public void selectBhmxls(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        renderJson(srv.selectBhmxls(kv));
    }

    /**
     * 总部停补商品
     */
    public void zbtbsp(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        kv.set("userCode", getLoginErpUsercode());
        renderJson(srv.zbtbsp(kv));
    }
}
